package com.yc.navy.goods.service.lmpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.lucene.document.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yc.navy.goods.bean.FindByPageInfo;
import com.yc.navy.goods.bean.ProductInfo;
import com.yc.navy.goods.dto.ProductDTODTO;
import com.yc.navy.goods.dto.ProductInfoDTO;
import com.yc.navy.goods.dto.ProductInfoLuceneDTO;
import com.yc.navy.goods.enums.ResultEnum;
import com.yc.navy.goods.mapper.IProductInfoMapper;
import com.yc.navy.goods.mapper.ITypeInfoMapper;
import com.yc.navy.goods.service.IProductInfoService;
import com.yc.navy.goods.util.FindByPageParamUtil;
import com.yc.navy.goods.util.LucenceUtil;
import com.yc.navy.goods.util.StringUtil;
import com.yc.navy.goods.vo.ResultVO;
import com.yc.navy.goods.vo.StreamVO;

@Service
public class ProductInfoServiceImpl implements IProductInfoService{
	@Autowired
	private IProductInfoMapper productInfoMapper;
	
	@Autowired
	private ITypeInfoMapper typeInfoMapper;

	@Override
	public int add(ProductInfo p) {
		if(StringUtil.checkNull(p.getPname(),p.getTid(),p.getPrice(),p.getPics(),p.getBnum(),p.getBsize(),p.getBsize())) {
			return -1;
		}
		return productInfoMapper.add(p);
	}

	@Override
	public int addbalance(ProductInfo p) {
		if(StringUtil.checkNull(p.getBcolor(),p.getBsize(),p.getBnum())) {
			return -1;
		}
		return productInfoMapper.addbalance(p);
	}

	@Override
	public List<ProductInfo> finds() {
		return productInfoMapper.finds();
	}

	@Override
	public StreamVO findByPageForBack(Map<String, Object> map) {
		List<ProductInfoDTO> list = productInfoMapper.findByPage(FindByPageParamUtil.StreamVOfindByPageParam(map));
		if(list == null || list.isEmpty()) {
			return new StreamVO(ResultEnum.NOTFOUND);
		}
		return new StreamVO(list,productInfoMapper.total());
	}

	@Override
	public List<ProductDTODTO> lists() {
		return productInfoMapper.lists();
	}

	@Override
	public List<ProductDTODTO> list(int tid) {
		return productInfoMapper.list(tid);
	}

	@Override
	public List<ProductDTODTO> findone(int pid) {
		return productInfoMapper.findone(pid);
	}

	@Override
	public List<ProductDTODTO> findlist(int cid) {
		return productInfoMapper.findlist(cid);
	}

	@Override
	public Map<String, Object> find(String pname) {
		List<ProductInfoLuceneDTO> list = productInfoMapper.find();
		
		//初始化lucene 工具
		LucenceUtil lu = new LucenceUtil("productInfoLuceneDTO","pid", new String[] {"pname","type","model"});
		
		//清空字典
		lu.deleteAll();
		
		//添加
		lu.add(ProductInfoLuceneDTO.class, list);
		
		//搜索
		List<Document> docs = lu.search(new String[] {"pname","type","model"}, pname, 100); 
		System.out.println(docs);
		
		//取值
		List<String> pnos = docs.stream().map(item -> item.get("pid")).collect(Collectors.toList());
		Map<String, Object> result = new HashMap<String, Object>();
		if(pnos.isEmpty()) {
			result.put("code", "600");
			return result;
		}
		result.put("code", 200);
		result.put("types", typeInfoMapper.finds());
		result.put("product", productInfoMapper.findList(pnos));
		return result;
	}

	@Override
	public List<ProductDTODTO> findListnow() {
		return productInfoMapper.findListnow();
	}

	@Override
	public List<ProductInfoDTO> productLists(FindByPageInfo find) {
		return productInfoMapper.productList(find);
	}

	@Override
	public Integer producttotal(Integer sid) {
		return productInfoMapper.producttotal(sid);
	}

	@Override
	public List<ProductInfoDTO> productIdList(FindByPageInfo find) {
		return productInfoMapper.productIdList(find);
	}

	@Override
	public Integer findShopid(Integer pid) {
		if(StringUtil.checkNull(String.valueOf(pid))) {
			return -1;
		}
		return productInfoMapper.findShopid(pid);
	}

	@Override
	public List<ProductInfo> findShopList(Integer sid) {
		if(StringUtil.checkNull(String.valueOf(sid))) {
			return null;
		}
		return productInfoMapper.findShopList(sid);
	}

	@Override
	public Integer findBackProductsCount() {
		return productInfoMapper.findBackProductsCount();
	}

	@Override
	public ResultVO findBackProductsList(FindByPageInfo find) {
		List<ProductInfo> list = productInfoMapper.findBackProductsList(find);
		if(list.isEmpty() || list.size() <= 0) {
			return new ResultVO(ResultEnum.ERROR);
		}
		return new ResultVO(ResultEnum.SUCCESS,list);
	}

	@Override
	public Integer updateStatus(ProductInfo product) {
		if(StringUtil.checkNull(String.valueOf(product.getPstatus()),String.valueOf(product.getPid()))) {
			return -1;
		}
		return productInfoMapper.updateStatus(product);
	}

	@Override
	public Integer updateProductInfo(ProductInfoDTO productDTO) {
		if(StringUtil.checkNull(String.valueOf(productDTO.getPid()),String.valueOf(productDTO.getPname()),String.valueOf(productDTO.getIntro()),String.valueOf(productDTO.getPid()))) {
			return -1;
		}
		return productInfoMapper.updateProductInfo(productDTO);
	}

	@Override
	public Integer checkShopid(Long pid) {
		if(StringUtil.checkNull(String.valueOf(pid))) {
			return -1;
		}
		return productInfoMapper.checkShopid(pid);
	}

}
